package com.example.xianji.adapter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import com.example.xianji.R;
import com.example.xianji.bean.news;
import com.example.xianji.utils.ImageLoadUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.core.imageaware.ImageViewAware;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class product_Adapter extends BaseAdapter {
    /**************************选型大厅列表适配器****************************/
    private Context context;
    private ArrayList<news> list;
    private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();
    private DisplayImageOptions options;

    public product_Adapter(Context context, ArrayList<news> list) {
        super();
        this.context = context;
        this.list = list;
        options = new DisplayImageOptions.Builder()
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .considerExifParams(true)
                .bitmapConfig(Bitmap.Config.RGB_565)//设置为RGB565比起默认的ARGB_8888要节省大量的内存
                .delayBeforeLoading(100)//载入图片前稍做延时可以提高整体滑动的流畅度
                .displayer(new RoundedBitmapDisplayer(20)).build();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder mHolder;
        if (convertView == null) {
            mHolder = new Holder();
            convertView = View.inflate(context, R.layout.product_item, null);
            mHolder.product_name = (TextView) convertView.findViewById(R.id.product_name);
            mHolder.product_tag = (TextView) convertView.findViewById(R.id.product_tag);
            mHolder.product_image = (ImageView) convertView.findViewById(R.id.product_image);
            convertView.setTag(mHolder);
        }
        mHolder = (Holder) convertView.getTag();
        mHolder.product_name.setText(list.get(position).getNews_title());
        mHolder.product_tag.setText(list.get(position).getName());
        //显式的将checkActualViewSize设为false, 这样图片的缓存也将只会保存一个副本,保证第二次查询时可以直接命中
        ImageViewAware imageviewawre = new ImageViewAware(mHolder.product_image, false);
        ImageLoadUtil.loadImageByPath(options, list.get(position).getPath(), imageviewawre, animateFirstListener);
        return convertView;
    }

    class Holder {
        TextView product_name, product_tag;
        ImageView product_image;
    }

    public static class AnimateFirstDisplayListener extends SimpleImageLoadingListener {
        public static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());

        @Override
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
            if (loadedImage != null) {
                ImageView imageView = (ImageView) view;
                boolean firstDisplay = !displayedImages.contains(imageUri);
                if (firstDisplay) {
                    FadeInBitmapDisplayer.animate(imageView, 200);
                    displayedImages.add(imageUri);
                }
            }
        }
    }
}
